/*
 * 文件名: MyDatePicker.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XIAOFANQING001
 * 创建时间: 2011-12-29
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget;

import java.util.Calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import app.utils.CommonUtils;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;
import app.widget.calendar.widget.adapter.DateNumericAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XIAOFANQING001
 * @date 2011-12-29
 * @version [Android PABank C01, @2011-12-29]
 * @description
 */
public class MyDatePicker extends LinearLayout implements OnWheelChangedListener,OnWheelScrollListener
{
	
    public static final String[] MONTHS = 
       {
        "1月", 
        "2月", 
        "3月", 
        "4月", 
        "5月",
        "6月", 
        "7月", 
        "8月", 
        "9月", 
        "10月", 
        "11月", 
        "12月"
       };
    
    private WheelView wvYear;
    private WheelView wvMonth;
    private WheelView wvDay;
    
    private LayoutInflater mInflater;
    
    private Calendar mCalendarToday =  CommonUtils.getCurrentTimeByCalendar(); //Calendar.getInstance();;
    
    
    private int mYear;
    private int mMonth;
    private int mDay;
    
    private int mCurrentYear;
    private int mCurrentMonth;
    private int mCurrentDay;
    
    private int minYear = 2000;
    private int maxYear = 2100;
    
    private OnDateChangerLister onDateChangerLister;
    
    private Context context;
    
    private boolean isScroll;
    
    private boolean isSelect = false;
    

	public MyDatePicker(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.context = context;
        init();
    }

    public MyDatePicker(Context context)
    {
        super(context);
        this.context = context;
        init();
    }
    
    private void init() {
        mInflater = LayoutInflater.from(getContext());
        
        View root = mInflater.inflate(R.layout.app_widget_my_date_picker, null);
        LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        root.setLayoutParams(lp);
        addView(root);
        
        wvYear = (WheelView) findViewById(R.id.wv_year);
        wvMonth = (WheelView) findViewById(R.id.wv_month);
        wvDay = (WheelView) findViewById(R.id.wv_day);
        
        mCurrentYear = mCalendarToday.get(Calendar.YEAR);
        mCurrentMonth = mCalendarToday.get(Calendar.MONTH);
        mCurrentDay = mCalendarToday.get(Calendar.DAY_OF_MONTH);
        
        mYear = mCurrentYear;
        mMonth = mCurrentMonth + 1;
        mDay = mCurrentDay + 1;
        
        wvYear.setViewAdapter(new DateNumericAdapter(getContext(), minYear , maxYear, mCurrentYear - minYear, "年"));
        wvMonth.setViewAdapter(new DateArrayAdapter(getContext(), MONTHS, mCurrentMonth));
        
        updateDays(wvYear,wvMonth,wvDay);
        wvDay.setViewAdapter(new DateNumericAdapter(getContext(), 1, mCalendarToday.getActualMaximum(Calendar.DAY_OF_MONTH), mCalendarToday.get(Calendar.DAY_OF_MONTH) - 1, "日"));
        
        //wvYear.setCurrentItem(mCurrentYear - minYear);
        //wvMonth.setCurrentItem(mCurrentMonth);
        //wvDay.setCurrentItem(mCalendarToday.get(Calendar.DAY_OF_MONTH) - 1);
        
        wvYear.addChangingListener(this);
        wvMonth.addChangingListener(this);
        wvDay.addChangingListener(this);
        
        wvYear.addScrollingListener(this);
        wvMonth.addScrollingListener(this);
        wvDay.addScrollingListener(this);
    }
    
    
    public void select(int year, int month, int day) {
    	Log.i("zbkc", "year:"+year+"month:"+month+"day:"+day);
    	isSelect = true;
        wvYear.setCurrentItem(year - minYear);
        
        wvMonth.setCurrentItem(month);
        wvDay.setCurrentItem(day - 1);
        
        mYear = year;
        mMonth = month ;
        mDay = day;
    }
    
    public boolean isScroll() {
		return isScroll;
	}

	public void setScroll(boolean isScroll) {
		this.isScroll = isScroll;
	}
  
    public void setMinYear(int year) {
        minYear = year;
    }
    
    public void setMaxYear(int year) {
        maxYear = year;
    }
    
    public void setOnDateChangerLister(OnDateChangerLister onDateChangerLister)
    {
        this.onDateChangerLister = onDateChangerLister;
    }

    public OnDateChangerLister getOnDateChangerLister()
    {
        return onDateChangerLister;
    }

    public interface OnDateChangerLister {
        public void onDateChange(int year, int month, int day);
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue)
    {
    	
        int id = wheel.getId();
        switch (id) {
            case R.id.wv_year:
                
                mYear = minYear + newValue;
                break;
            case R.id.wv_month:
                
                mMonth = newValue ;
                break;
            case R.id.wv_day:
                
                mDay = newValue + 1;
                break;
        }
        
        updateDays(wvYear,wvMonth,wvDay);
        
        Log.i("zbkc", "mYear:"+mYear+"mMonth:"+mMonth+"mDay:"+mDay);
        
        if (onDateChangerLister!=null&&!isSelect){
        	onDateChangerLister.onDateChange(mYear, mMonth, mDay);  
        	
        }
        
        /*if (onDateChangerLister != null ) {
            handler.post(new Runnable()
            {
                @Override
                public void run()
                {
                	      
                }
            });
        }*/
        
        
    }
    
   // private Handler handler = new Handler();
    /**
     * 
     * [一句话功能简述]<BR>
     * [功能详细描述]
     * @param context 
     * @param year
     * @param month
     * @param day
     */
    
    private void updateDays( WheelView year,
			WheelView month, WheelView day) {
		//Calendar c =  CommonUtils.getCurrentTimeByCalendar(); 
		
		Calendar calendar = Calendar.getInstance();;
		calendar.clear();
		calendar.set(Calendar.YEAR,
				mCalendarToday.get(Calendar.YEAR) + year.getCurrentItem());
		calendar.set(Calendar.MONTH, month.getCurrentItem());

		int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		day.setViewAdapter(new DateNumericAdapter(context, 1, maxDays, calendar
				.get(Calendar.DAY_OF_MONTH) - 1, "日"));
		int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
		day.setCurrentItem(curDay - 1, true);
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		isSelect = false;
	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
		
		if(isScroll&&mYear>mCurrentYear){
			
			//Log.i("zbkc", "isScroll mYear:"+mYear+"mCurrentYear:"+mCurrentYear);
			wvYear.scroll(mCurrentYear-mYear, 0);
			wvYear.invalidate();
			
		}
		if(isScroll&&mYear>=mCurrentYear&&mMonth>mCurrentMonth){
			
			//Log.i("zbkc", "isScroll mMonth:"+mMonth+"mCurrentMonth:"+mCurrentMonth);
			wvMonth.scroll(mCurrentMonth-mMonth, 0);
			wvMonth.invalidate();
		}
		
		if(isScroll&&mYear>=mCurrentYear&&mMonth>=mCurrentMonth&&mDay>mCurrentDay){
			//Log.i("zbkc", "isScroll mDay:"+mDay+"mCurrentDay:"+mCurrentDay);
			wvDay.scroll(mCurrentDay-mDay, 0);
			wvDay.invalidate();
		}
		
	}

}

